﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FilStartUps.FILAPI;

namespace FilStartUps
{
    public partial class Search : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ViewState["Skip"] = 0;
                GetSearch();
            }
        }

        private void GetSearch()
        {
            string searchString = HttpUtility.UrlDecode(Request.QueryString["q"]);
            var search = FilStartUpsAPI.GetSearchItem(searchString);
            ViewState["search"] = search;
            BindDataList(Convert.ToInt32(ViewState["Skip"]));

            if (search.Count() > 5)
            {
                lnkbNext.Visible = true;
                lnkbPrev.Visible = true;
                lnkbPrev.Enabled = false;
            }
            else
            {
                lnkbNext.Visible = false;
                lnkbPrev.Visible = false;
            }
            
        }
        public string ConvertDate(string date)
        {
            string date_;
            try
            {
                DateTime _date = Convert.ToDateTime(date);
                date_ = _date.ToShortDateString();//String.Format("{0:d/M/yyyy}", date);
            }
            catch
            {
                date_ = date.ToString();
            }
            return date_;
        }


        private void BindDataList(int Skip)
        {
            int iPage = Skip + 5;
            int currPage = iPage / 5;
            double TotalPage;
            
            string searchString = HttpUtility.UrlDecode(Request.QueryString["q"]);

            var search = FilStartUpsAPI.GetSearchItem(searchString);

            TotalPage = Math.Ceiling((double)search.Count() / 5);

            var searchItem = search.Skip(Skip).Take(5);

            if (iPage > search.Count())
            {
                lnkbNext.Enabled = false;
            }
            else
            {
                lnkbNext.Enabled = true;
            }

            dlsearchresult.DataSource = searchItem;
            dlsearchresult.DataBind();
            litPageNumber.Text = "Page " + currPage.ToString().Trim() + " of " + (int)TotalPage;

        }
        //Tuncate Description
        public string Truncate(string Description)
        {
            string desc;

            try
            {
                desc = Description.ToString().Substring(0, 100) + "...";
            }
            catch (Exception)
            {
                desc = Description;
            }
            return desc;
        }

        protected void lnkbNext_Click(object sender, EventArgs e)
        {
            lnkbPrev.Enabled = true;
            ViewState["Skip"] = Convert.ToInt32(ViewState["Skip"]) + 5;
            BindDataList(Convert.ToInt32(ViewState["Skip"]));
        }

        protected void lnkbPrev_Click(object sender, EventArgs e)
        {
            ViewState["Skip"] = Convert.ToInt32(ViewState["Skip"]) - 5;
            BindDataList(Convert.ToInt32(ViewState["Skip"]));
            if (Convert.ToInt32(ViewState["Skip"]) == 0)
            {
                lnkbPrev.Enabled = false;
            }
        }        
      
    }
}